package com.hrbxlh.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.Sheet;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fasterxml.jackson.databind.ObjectMapper;



/**
 * 公共方法
 * 作者：商莹
 * 日期：2017-1-13
 * 
 */
public class PubMethods {

	/**
	 * 创建当前时间
	 * 参数：时间、区分是要当前时间还是传过来的时间
	 * 
	 */
	public static String getNowTime(Date date){
		String nowtime = "";
		if(date!=null){
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
			nowtime = sdf1.format(date);
			System.out.println("当前时间是"+nowtime);
		}
		return nowtime;
	}

	//判断是否包含括号
	public static String isContailKh(String str) {
		if (str.endsWith("()")||str.endsWith("（）")) {
			str = str.replace("()", "（  ）");
			return str;
		}
		if (!str.endsWith("()")&&!str.endsWith("（）")) {
			str = str.concat("（  ）");
			return str;
		}
		return str;
	}

	/***@description 从session里取值
	 * @param 数组
	 * @throws Exception
	 */
	public static Map<String, Object> getSessionValues(String[] keysArray, HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			HttpSession session = request.getSession();
			System.out.println("session"+session.getAttribute("randomString"));
			for(int i = 0; i < keysArray.length; i++){
				System.out.println("keysArray[i]"+keysArray[i]);
				System.out.println(" (String)session.getAttribute(keysArray[i])"+ (String)session.getAttribute(keysArray[i]));
				map.put(keysArray[i], (String)session.getAttribute(keysArray[i]));
			}
		}catch(Exception e){
			throw e;
		}
		System.out.println("最后返回的map"+map);
		return map;
	}
	
	//设置excel基本的样式设置
	public static HSSFCellStyle cssStyle(HSSFWorkbook workbook){
		 HSSFCellStyle style = workbook.createCellStyle();
		    HSSFFont font = workbook.createFont();
		    font.setFontHeight((short)220);  
		    font.setFontName("宋体"); 
//		    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
		    style.setFont(font);
		    return style;
	}
	
	//excel的反射方法
	public static String getFieldValueByName(String fieldName, Map<String,Object> map){
		//fieldName:className,obj:map对象
		String value = "";
        try {
            value = String.valueOf(map.get(fieldName));
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("fieldName"+fieldName);
            System.out.println("属性不存在！");
        }
        return value;
	}
	
	//导出excel的具体操作设置
	public static void excelOut(String fileName,HttpServletResponse response,HSSFWorkbook workbook) throws IOException{
		fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1") + ".xls";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
	}
	//判断手机号是否合法
	 public static boolean isMobileNum(String telNum){  
	        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");  
	        Matcher m = p.matcher(telNum);  
	        return m.matches();  
	    }
	 
	 //去掉字符串指定字符
	 public static String getCharStr(String str){
		  int i=str.indexOf("、");
		  str=str.substring(0, i)+str.substring(i+1);
		  System.out.println(str);
		  return str;
	 }
	 
	 //导入试题获取有效行数
	 public static int getNewRows(int rows,Sheet rs){
		 int newRow=0;
         boolean a=false;
         for(int i=1;i<rows;i++){
			   String Name=rs.getCell(2, i).getContents().trim();
//			   System.out.println("name"+Name);
			   if(Name!=null&&Name!=""){
				   a=true;
			   }else{
				   break;
			   }
	    	   if(a){
	    		   newRow = newRow+1; 
	    	   }
      	   
         }
         System.out.println("有效的行数"+(newRow+1));
         return (newRow+1);
	 }
	 
	//导入成绩获取有效行数
	 public static int getSourceNewRows(int rows,Sheet rs){
		 int newRow=0;
         boolean a=false;
         for(int i=0;i<rows;i++){
			   String Name=rs.getCell(1, i).getContents().trim();
			   if(Name!=null&&Name!=""){
				   a=true;
			   }else{
				   break;
			   }
	    	   if(a){
	    		   newRow = newRow+1; 
	    	   }
      	   
         }
         System.out.println("有效的行数"+newRow);
         return newRow;
	 }
	 
	//导入学生人员获取有效行数
	 public static int getMemNewRows(int rows,Sheet rs){
		 int newRow=0;
         boolean a=false;
         for(int i=5;i<rows;i++){
			   String Name=rs.getCell(1, i).getContents().trim();
			   System.out.println("Name"+Name);
			   if(Name!=null&&Name!=""){
				   a=true;
			   }else{
				   break;
			   }
	    	   if(a){
	    		   newRow = newRow+1; 
	    	   }
      	   
         }
         System.out.println("有效的行数"+(newRow+1));
         return (newRow+1);
	 }
	 
	//导入教师人员获取有效行数
	 public static int getTeaMemNewRows(int rows,Sheet rs){
		 int newRow=0;
         boolean a=false;
         for(int i=2;i<rows;i++){
			   String Name=rs.getCell(1, i).getContents().trim();
			   System.out.println("Name"+Name);
			   if(Name!=null&&Name!=""){
				   a=true;
			   }else{
				   break;
			   }
	    	   if(a){
	    		   newRow = newRow+1; 
	    	   }
      	   
         }
         System.out.println("有效的行数"+(newRow+1));
         return (newRow+1);
	 }
	 
	//判断一个字符串是否是中文
	public static boolean isStrChinese(String str) {
	    if (str == null) return false;
	    for (char c : str.toCharArray()) {
	        if (isChinese(c)) return true;// 有一个中文字符就返回
	    }
		return false;
	}
	
	//判断一个字符是否是
	public static boolean isChinese(char c) {
	      return c >= 0x4E00 &&  c <= 0x9FA5;// 根据字节码判断
	}
}
